package com.dongdongshop.controller;

import com.dongdongshop.data.Result;
import com.dongdongshop.enums.ResultEnums;
import com.dongdongshop.pojo.PageResult;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.service.TbContentService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("TbContent")
public class TbContentController {

    //注入service
    @DubboReference
    private TbContentService tbContentService;
    @Autowired
    private RedisTemplate redisTemplate;

    //查询全部广告列表(要求分页)
    @ResponseBody
    @RequestMapping("TbContentshowall")
    public Result TbContentshowall( @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "2") Integer pageSize) {
        try {
            PageResult<TbContent> tbBrands = tbContentService.TbContentshowall(pageNum, pageSize);
            return Result.response(ResultEnums.SHOW_SUCCESS).setData(tbBrands);
        } catch (Exception e) {
            System.out.println(e);
            return Result.response(ResultEnums.SHOW_ERROR);
        }
    }

    //添加广告
    @ResponseBody
    @RequestMapping("addTbContent")
    public Result addTbContent(TbContent tbContent) {
        try {
            tbContentService.addTbContent(tbContent);
            redisTemplate.delete("shuju");
            return Result.response(ResultEnums.ADD_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.ADD_ERROR);
        }
    }

    //回显单个广告数据
    @ResponseBody
    @RequestMapping("showecho")
    public Result showecho(Long id) {
        try {
            TbContent tbBrand = tbContentService.showecho(id);
            return Result.response(ResultEnums.ECHO_SUCCESS).setData(tbBrand);
        } catch (Exception e) {
            return Result.response(ResultEnums.ECHO_ERROR);
        }
    }

    //修改单个数据
    @ResponseBody
    @RequestMapping("updateTbContent")
    public Result updateTbContent(TbContent tbBrand) {
        try {
            tbContentService.updateTbContent(tbBrand);
            redisTemplate.delete("shuju");
            return Result.response(ResultEnums.UPDATE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.UPDATE_ERROR);
        }
    }

    //删除单个数据
    @ResponseBody
    @RequestMapping("deleteTbContent")
    public Result deleteTbContent(Long id) {
        try {
            tbContentService.deleteTbContent(id);
            redisTemplate.delete("shuju");
            return Result.response(ResultEnums.DELETE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.DELETE_ERROR);
        }
    }

    //删除多个数据
    @ResponseBody
    @RequestMapping("deleteallTbContent")
    public Result deleteallTbContent(@RequestParam(value = "ids[]") List<Long> ids) {
        try {
            tbContentService.deleteallTbContent(ids);
            redisTemplate.delete("shuju");
            return Result.response(ResultEnums.DELETE_SUCCESS);
        } catch (Exception e) {
            return Result.response(ResultEnums.DELETE_ERROR);
        }
    }
}
